﻿// HSS.ServiceModel.Internal.VirtualInspectorBehavior.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       VirtualInspectorBehavior.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.ServiceModel.Internal
{
	#region Using Directives
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.Linq;
	using System.ServiceModel;
	using System.ServiceModel.Channels;
	using System.ServiceModel.Description;
	using System.ServiceModel.Dispatcher;
	#endregion

	#region VirtualInspectorBehavior
#if !DEBUG
	[System.Diagnostics.DebuggerStepThroughAttribute()]
#endif
	internal class VirtualInspectorBehavior : IServiceBehavior
	{
		List<IDispatchMessageInspector> inspectors;
		string contract = string.Empty;

		public VirtualInspectorBehavior(string contractName, IDispatchMessageInspector customInspector)
		{
			this.contract = contractName;
			this.inspectors = new List<IDispatchMessageInspector>();
			this.inspectors.Add(customInspector);
		}

		public void AddInspector(IDispatchMessageInspector customInspector)
		{
			this.inspectors.Add(customInspector);
		}

		#region IServiceBehavior Members

		public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
		{
			return;
		}

		public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
		{
			foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
			{
				foreach (EndpointDispatcher endPoint in dispatcher.Endpoints)
				{
					if (null != endPoint)
					{
						foreach (var inspector in this.inspectors)
							endPoint.DispatchRuntime.MessageInspectors.Add(inspector);
						return;
					}
				}
			}
		}

		public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
		{

		}

		#endregion
	}
	#endregion
}